package com.example.qinghe.frontController.communication;


import com.alibaba.fastjson.JSON;
import com.example.qinghe.entity.communication.Staff;
import com.example.qinghe.service.communication.StaffService;
import com.example.qinghe.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/front/main")
@Validated
@CrossOrigin(origins = "*", maxAge = 3600,allowCredentials="true")
@Api(tags = "登录方法，文件上传方法", description = "MainController")
public class MainController {
    @Value("${upload-path}")
    private String path;
    @Value("${upload-qianzhui}")
    private String qianzhui;
    @Value("${upload-type}")
    private int type;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private StaffService staffService;

    @ApiOperation("文件上传")
    @PostMapping("/upload")
    public Result upload2(@RequestParam(name="file") MultipartFile file){
        Map<String,Object> m=new HashMap<>();
        if (file!=null &&!file.isEmpty()) {
            File f = new File(path);
            if (!f.exists())
                f.mkdirs();
            //原来的文件名
            String filename = file.getOriginalFilename();
            String houzhui = filename.substring(filename.lastIndexOf('.'));
            String newName = UUID.randomUUID() + houzhui;

            try {
                file.transferTo(new File(f, newName));
                m.put("code",0);
                return Result.success("img/"+newName,"添加成功");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        m.put("code",415);
        m.put("msg","图片没有选择");
        return Result.error(415,"图片没有选择");
    }

    @ApiOperation("获取请求头")
    @GetMapping("/getHeader")
    public Result getHeader(){
        String key= UUID.randomUUID().toString();
        redisTemplate.opsForValue().set(key,null,300, TimeUnit.MINUTES);
        return Result.success(key);
    }

    @ApiOperation("数据转移，后端用的")
    @GetMapping("/move")
    public void move(){
        List<Staff> enterprises=staffService.selAll();
        for (int i = 0; i < enterprises.size(); i++) {
            System.out.println(JSON.toJSONString(enterprises.get(i)));
            if(enterprises.get(i)!=null&&enterprises.get(i).getEnterprise_id()!=null)
            {
                enterprises.get(i).setId(enterprises.get(i).getId()+100);
                if(enterprises.get(i).getEnterprise_id()!=1)
                    enterprises.get(i).setEnterprise_id(enterprises.get(i).getEnterprise_id()+100);
                staffService.addStaff(enterprises.get(i));
            }

        }
    }

}





